/*   1:    */ package com.atomikos.persistence.imp;
/*   2:    */ 
/*   3:    */ import com.atomikos.diagnostics.Console;
/*   4:    */ import com.atomikos.persistence.LogException;
/*   5:    */ import com.atomikos.persistence.LogStream;
/*   6:    */ import com.atomikos.persistence.ObjectImage;
/*   7:    */ import com.atomikos.persistence.ObjectLog;
/*   8:    */ import com.atomikos.persistence.Recoverable;
/*   9:    */ import java.io.PrintStream;
/*  10:    */ import java.util.Enumeration;
/*  11:    */ import java.util.Hashtable;
/*  12:    */ import java.util.Stack;
/*  13:    */ import java.util.Vector;
/*  14:    */ 
/*  15:    */ public class StreamObjectLog
/*  16:    */   implements ObjectLog
/*  17:    */ {
/*  18:    */   protected LogStream logstream_;
/*  19:    */   protected Hashtable logTable_;
/*  20:    */   protected long size_;
/*  21: 56 */   private boolean initialized_ = false;
/*  22: 57 */   protected boolean panic_ = false;
/*  23:    */   protected Console console_;
/*  24:    */   private long count_;
/*  25:    */   private long maxCount_;
/*  26:    */   
/*  27:    */   private StreamObjectLog() {}
/*  28:    */   
/*  29:    */   public StreamObjectLog(LogStream logstream, long checkpointInterval, Console console)
/*  30:    */   {
/*  31: 93 */     this.logstream_ = logstream;
/*  32: 94 */     this.size_ = 0L;
/*  33: 95 */     this.console_ = console;
/*  34: 96 */     this.logTable_ = new Hashtable();
/*  35: 97 */     this.maxCount_ = checkpointInterval;
/*  36: 98 */     this.count_ = 0L;
/*  37:    */   }
/*  38:    */   
/*  39:    */   private synchronized void writeCheckpoint()
/*  40:    */     throws LogException
/*  41:    */   {
/*  42:108 */     this.count_ += 1L;
/*  43:109 */     if (this.count_ >= this.maxCount_)
/*  44:    */     {
/*  45:110 */       this.logstream_.writeCheckpoint(this.logTable_.elements());
/*  46:111 */       this.count_ = 0L;
/*  47:    */     }
/*  48:    */   }
/*  49:    */   
/*  50:    */   public synchronized void init()
/*  51:    */     throws LogException
/*  52:    */   {
/*  53:122 */     Stack errors = new Stack();
/*  54:123 */     Vector recovered = null;
/*  55:125 */     if (this.initialized_) {
/*  56:126 */       return;
/*  57:    */     }
/*  58:    */     try
/*  59:    */     {
/*  60:129 */       recovered = this.logstream_.recover();
/*  61:131 */       if (recovered != null)
/*  62:    */       {
/*  63:133 */         Enumeration entries = recovered.elements();
/*  64:134 */         while (entries.hasMoreElements())
/*  65:    */         {
/*  66:135 */           SystemLogImage entry = (SystemLogImage)entries.nextElement();
/*  67:138 */           if (entry.getId() != null) {
/*  68:140 */             if (!entry.isForgettable())
/*  69:    */             {
/*  70:141 */               if (!this.logTable_.containsKey(entry.getId())) {
/*  71:142 */                 this.size_ += 1L;
/*  72:    */               }
/*  73:145 */               this.logTable_.put(entry.getId(), entry);
/*  74:    */             }
/*  75:147 */             else if (this.logTable_.containsKey(entry.getId()))
/*  76:    */             {
/*  77:154 */               this.logTable_.remove(entry.getId());
/*  78:    */               
/*  79:    */ 
/*  80:    */ 
/*  81:158 */               this.size_ -= 1L;
/*  82:    */             }
/*  83:    */           }
/*  84:    */         }
/*  85:    */       }
/*  86:    */     }
/*  87:    */     catch (LogException le)
/*  88:    */     {
/*  89:168 */       throw le;
/*  90:    */     }
/*  91:    */     catch (Exception e)
/*  92:    */     {
/*  93:172 */       errors.push(e);
/*  94:173 */       throw new LogException(e.getMessage(), errors);
/*  95:    */     }
/*  96:    */     finally
/*  97:    */     {
/*  98:178 */       this.initialized_ = true;
/*  99:    */     }
/* 100:185 */     this.logstream_.writeCheckpoint(this.logTable_.elements());
/* 101:    */   }
/* 102:    */   
/* 103:    */   public synchronized Vector recover()
/* 104:    */     throws LogException
/* 105:    */   {
/* 106:194 */     Stack errors = new Stack();
/* 107:195 */     Vector hist = new Vector();
/* 108:197 */     if (!this.initialized_) {
/* 109:198 */       throw new LogException("Not initialized");
/* 110:    */     }
/* 111:199 */     Enumeration enumm = this.logTable_.elements();
/* 112:201 */     while (enumm.hasMoreElements())
/* 113:    */     {
/* 114:202 */       SystemLogImage next = (SystemLogImage)enumm.nextElement();
/* 115:    */       
/* 116:204 */       hist.addElement(next.getObjectImage().restore());
/* 117:    */     }
/* 118:207 */     return hist;
/* 119:    */   }
/* 120:    */   
/* 121:    */   public synchronized void flush(Recoverable rec)
/* 122:    */     throws LogException
/* 123:    */   {
/* 124:217 */     if (rec == null) {
/* 125:218 */       return;
/* 126:    */     }
/* 127:220 */     SystemLogImage simg = new SystemLogImage(rec, false);
/* 128:221 */     flush(simg, true);
/* 129:    */   }
/* 130:    */   
/* 131:    */   protected synchronized void flush(SystemLogImage img, boolean shouldSync)
/* 132:    */     throws LogException
/* 133:    */   {
/* 134:228 */     Stack errors = new Stack();
/* 135:230 */     if (img == null) {
/* 136:231 */       return;
/* 137:    */     }
/* 138:234 */     if (this.panic_) {
/* 139:235 */       throw new LogException("StreamObjectLog: PANIC");
/* 140:    */     }
/* 141:    */     try
/* 142:    */     {
/* 143:    */       try
/* 144:    */       {
/* 145:240 */         this.logstream_.flushObject(img, shouldSync);
/* 146:241 */         writeCheckpoint();
/* 147:    */       }
/* 148:    */       catch (LogException ioerr)
/* 149:    */       {
/* 150:244 */         ioerr.printStackTrace();
/* 151:245 */         errors.push(ioerr);
/* 152:    */         try
/* 153:    */         {
/* 154:251 */           this.logstream_.writeCheckpoint(this.logTable_.elements());
/* 155:    */         }
/* 156:    */         catch (Exception e)
/* 157:    */         {
/* 158:253 */           errors.push(e);
/* 159:    */         }
/* 160:255 */         throw new LogException(ioerr.getMessage(), errors);
/* 161:    */       }
/* 162:261 */       if (img.isForgettable())
/* 163:    */       {
/* 164:262 */         if (this.logTable_.containsKey(img.getId()))
/* 165:    */         {
/* 166:264 */           this.logTable_.remove(img.getId());
/* 167:265 */           this.size_ -= 1L;
/* 168:    */         }
/* 169:    */       }
/* 170:    */       else
/* 171:    */       {
/* 172:270 */         if (!this.logTable_.containsKey(img.getId())) {
/* 173:271 */           this.size_ += 1L;
/* 174:    */         }
/* 175:274 */         this.logTable_.put(img.getId(), img);
/* 176:    */       }
/* 177:    */     }
/* 178:    */     catch (LogException le)
/* 179:    */     {
/* 180:280 */       System.err.println("Error in StreamObjectLog.flush() " + le.getMessage());
/* 181:    */       
/* 182:282 */       throw le;
/* 183:    */     }
/* 184:    */     catch (Exception e)
/* 185:    */     {
/* 186:285 */       System.err.println("Error in StreamObjectLog.flush() " + e.getMessage());
/* 187:    */       
/* 188:    */ 
/* 189:288 */       errors.push(e);
/* 190:289 */       throw new LogException(e.getMessage(), errors);
/* 191:    */     }
/* 192:    */   }
/* 193:    */   
/* 194:    */   public synchronized Recoverable recover(Object id)
/* 195:    */     throws LogException
/* 196:    */   {
/* 197:299 */     if (!this.logTable_.containsKey(id)) {
/* 198:300 */       return null;
/* 199:    */     }
/* 200:301 */     SystemLogImage simg = (SystemLogImage)this.logTable_.get(id);
/* 201:302 */     return simg.getObjectImage().restore();
/* 202:    */   }
/* 203:    */   
/* 204:    */   public synchronized void delete(Object id)
/* 205:    */     throws LogException
/* 206:    */   {
/* 207:312 */     SystemLogImage previous = (SystemLogImage)this.logTable_.get(id);
/* 208:313 */     if (previous == null) {
/* 209:317 */       return;
/* 210:    */     }
/* 211:319 */     Recoverable bogus = previous.getRecoverable();
/* 212:320 */     SystemLogImage simg = new SystemLogImage(bogus, true);
/* 213:321 */     flush(simg, false);
/* 214:    */   }
/* 215:    */   
/* 216:    */   public synchronized void close()
/* 217:    */     throws LogException
/* 218:    */   {
/* 219:330 */     Stack errors = new Stack();
/* 220:    */     try
/* 221:    */     {
/* 222:332 */       if (this.logstream_ != null) {
/* 223:333 */         this.logstream_.close();
/* 224:    */       }
/* 225:335 */       this.initialized_ = false;
/* 226:    */     }
/* 227:    */     catch (LogException le)
/* 228:    */     {
/* 229:339 */       throw le;
/* 230:    */     }
/* 231:    */     catch (Exception e)
/* 232:    */     {
/* 233:341 */       e.printStackTrace();
/* 234:342 */       errors.push(e);
/* 235:343 */       throw new LogException(e.getMessage(), errors);
/* 236:    */     }
/* 237:    */   }
/* 238:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\transactions\3.7.0\transactions-3.7.0.jar
 * Qualified Name:     com.atomikos.persistence.imp.StreamObjectLog
 * JD-Core Version:    0.7.0.1
 */